package com.dkm.ec.home.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.dkm.base.api.model.Page;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.gl.domain.GlText;
import com.dkm.biz.gl.repository.GlTextRepository;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.home.domain.HomeEntity;
import com.dkm.ec.home.persistence.dao.HomeEntityQueryDao;
import com.dkm.ec.home.persistence.entity.HomeEntityPo;
import com.dkm.ec.home.repository.HomeEntityRepository;
import com.dkm.ec.product.persistence.dao.ProdRelQueryDao;
import com.dkm.ec.product.persistence.dao.ProductMediaQueryDao;
import com.dkm.ec.product.persistence.entity.ProdRelPo;
import com.dkm.ec.product.persistence.entity.ProductMediaPo;

/**
 * 对象功能:家居产品 Repository接口的实现类
 * 开发公司:SKG.COM
 * 开发人员:颜超敏
 * 创建时间:2014-12-17 11:31:17
 */
@Repository
public class HomeEntityRepositoryImpl extends AbstractRepository<String, HomeEntityPo,HomeEntity> implements HomeEntityRepository{
	  
	@Resource
	private  HomeEntityQueryDao homeEntityQueryDao;
	@Resource
	private  ProdRelQueryDao prodRelQueryDao;
	@Resource
	private ProductMediaQueryDao productMediaQueryDao;
	
	 @Resource
	 private GlTextRepository glTextRepository;

	public HomeEntity newInstance() {
		HomeEntityPo po = new HomeEntityPo();
		HomeEntity homeEntity = SpringHelper.getBean(HomeEntity.class);
		homeEntity.setData(po);
		return homeEntity;
	}

	public HomeEntity newInstance(HomeEntityPo po) {
		HomeEntity homeEntity = SpringHelper.getBean(HomeEntity.class);
		homeEntity.setData(po);
		return homeEntity;
	} 
	
	@Override
	protected IQueryDao<String, HomeEntityPo> getQueryDao() {
		return homeEntityQueryDao;
	}

	public List<HomeEntityPo> findRelByProdKey(String prodId) {
		if(StringUtils.isEmpty(prodId)) return null;
		Map<String, Object> params = new HashMap<String, Object>();
		String sqlWhere="";
		sqlWhere += " MAIN_ID_='"+prodId+"'" ;
		params.put("whereSql",sqlWhere);
		List<ProdRelPo> prodRelPos = prodRelQueryDao.findAll(params);
		List<String> relIds = new ArrayList<String>();
		for(ProdRelPo prodRelPo:prodRelPos){
			if(relIds.contains(prodRelPo.getRelId())){
				continue;
			}else{
				relIds.add(prodRelPo.getRelId());
			}
		}
		
		List<HomeEntityPo> relHomeEntityPos = homeEntityQueryDao.findByIds(relIds);
		for(HomeEntityPo po:relHomeEntityPos){
			Map<String, Object> param = new HashMap<String, Object>();
			sqlWhere="";
			sqlWhere += " PROD_ID_='"+po.getId()+"'" ;
			sqlWhere += " and IS_MAIN_='Y'";
			param.put("whereSql",sqlWhere);
			List<ProductMediaPo> productMediaPos = productMediaQueryDao.findAll(param);
			if(productMediaPos!=null&&productMediaPos.size()>0) {
				po.setMainImgPath(productMediaPos.get(0).getCloudPath());
			}
		}
		return relHomeEntityPos;
	}

	//前端户型详细信息
	public HomeEntityPo findDetailByKey(String key) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("key", key);
		HomeEntityPo po= homeEntityQueryDao.getByKey("findDetailByKey", params);
		GlText glText = glTextRepository.load(po.getDescFid());
		po.setDescContent(glText.getData().getText());
		if(po!=null){
			List<HomeEntityPo> associatHomeEntityPos=findAssociatHomeEntityPo(po.getHomeId());
			po.setAssociatHomeEntityPos(associatHomeEntityPos);
			
			List<ProductMediaPo> productMediaPos=productMediaQueryDao.findByProdId(po.getHomeId());
			po.setProductMediaPos(productMediaPos);
		}
		return po;
	}

	public List<HomeEntityPo> findAssociatHomeEntityPo(String homeId) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("homeId", homeId);
		return homeEntityQueryDao.findByKey("findAssociatHomeEntityPo", params);
	}
	
	public List<HomeEntityPo> findAssociatWholeHomeEntityPo(String homeId) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("homeId", homeId);
		return homeEntityQueryDao.findByKey("findAssociatWholeHomeEntityPo", params);
	}
	
	@Override
	public List<HomeEntityPo> findForSelectorPaged(Map<String, Object> map,Page page) {
	List<HomeEntityPo> homeEntityPos = homeEntityQueryDao.findByKey("findForSelect", map,page);
		return homeEntityPos;
	}

	@Override
	public Integer countForSelector(Map<String, Object> params) {
	return homeEntityQueryDao.countByKey("countForSelect", params);
	}

	@Override
	public List<HomeEntityPo> findForSelector(Map<String, Object> map) {
		List<HomeEntityPo> homeEntityPos = homeEntityQueryDao.findByKey("findForSelect", map);
		return homeEntityPos;
	}

	@Override
	public HomeEntityPo findHomeEntityPoByHomeId(String homeId) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("homeId", homeId);
		List<HomeEntityPo> homeEntityPos =homeEntityQueryDao.findByKey("findHomeEntityPoByHomeId", params);
		if(homeEntityPos!=null && homeEntityPos.size()!=0){
			return homeEntityPos.get(0);
		}
		return  null;
	}
}
